package com.wilmer.knowledges.controller;

import com.wilmer.common.annotation.Anonymous;
import com.wilmer.common.annotation.Log;
import com.wilmer.common.core.controller.BaseController;
import com.wilmer.common.core.domain.AjaxResult;
import com.wilmer.common.core.page.TableDataInfo;
import com.wilmer.common.enums.BusinessType;
import com.wilmer.common.utils.poi.ExcelUtil;
import com.wilmer.knowledges.domain.YwHomeKnowledge;
import com.wilmer.knowledges.service.IYwHomeKnowledgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = {"育儿知识库接口"})
@RestController
@RequestMapping("/knowledges/knowledges")
public class YwHomeKnowledgeController extends BaseController
{

    @Autowired
    private IYwHomeKnowledgeService ywHomeKnowledgeService;
    /**
     * 查询育儿知识库列表
     */
    @ApiOperation("查询育儿知识库列表")
    @GetMapping("/list")
    public TableDataInfo list(YwHomeKnowledge ywHomeKnowledge)
    {
        startPage();
        List<YwHomeKnowledge> list = ywHomeKnowledgeService.selectYwHomeKnowledgeList(ywHomeKnowledge);
        return getDataTable(list);
    }

    /**
     * 导出育儿知识库列表
     */
    @ApiOperation("导出育儿知识库列表")
    @Log(title = "育儿知识库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YwHomeKnowledge ywHomeKnowledge)
    {
        List<YwHomeKnowledge> list = ywHomeKnowledgeService.selectYwHomeKnowledgeList(ywHomeKnowledge);
        ExcelUtil<YwHomeKnowledge> util = new ExcelUtil<YwHomeKnowledge>(YwHomeKnowledge.class);
        util.exportExcel(response, list, "育儿知识库数据");
    }

    /**
     * 获取育儿知识库详细信息
     */
    @ApiOperation("获取育儿知识库详细信息")
    @GetMapping(value = "/{knowledgeId}")
    public AjaxResult getInfo(@PathVariable("knowledgeId") Long knowledgeId)
    {
        return success(ywHomeKnowledgeService.selectYwHomeKnowledgeByKnowledgeId(knowledgeId));
    }

    /**
     * 新增育儿知识库
     */
    @ApiOperation("新增育儿知识库")
    @Log(title = "育儿知识库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YwHomeKnowledge ywHomeKnowledge)
    {
        return toAjax(ywHomeKnowledgeService.insertYwHomeKnowledge(ywHomeKnowledge));
    }

    /**
     * 修改育儿知识库
     */
    @ApiOperation("修改育儿知识库")
    @Log(title = "育儿知识库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YwHomeKnowledge ywHomeKnowledge)
    {
        return toAjax(ywHomeKnowledgeService.updateYwHomeKnowledge(ywHomeKnowledge));
    }

    /**
     * 删除育儿知识库
     */
    @ApiOperation("删除育儿知识库")
    @Log(title = "育儿知识库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{knowledgeIds}")
    public AjaxResult remove(@PathVariable Long[] knowledgeIds)
    {
        return toAjax(ywHomeKnowledgeService.deleteYwHomeKnowledgeByKnowledgeIds(knowledgeIds));
    }

    /**
     * 统计育儿知识库列表文章数量
     */
    @ApiOperation("统计育儿知识库列表文章数量")
    @GetMapping("/count")
    public int count()
    {
        int count = ywHomeKnowledgeService.YwHomeKnowledgeCount();
        return count;
    }
}
